package com.mkl.ttxt.common;

import com.mkl.ttxt.common.bo.TtxtResult;
import com.mkl.ttxt.common.constant.BaseConstant;
import com.mkl.ttxt.common.exception.LoginException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public TtxtResult exceptionHandler(Exception ex) {
        //全局捕获异常记录
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.FAILED);
        if(ex instanceof LoginException){
            ttxtResult = new TtxtResult(BaseConstant.LOGIN_FAILED);
        }
        ttxtResult.setMessage("服务器异常:" + ex.getMessage());
        return ttxtResult;
    }
}
